package com.run;

import com.model.User;

import java.sql.*;

public class ManageRun {
    public boolean addUser(Connection con,String userName,String password) throws SQLException {
        String sql = "INSERT INTO USER VALUES (NULL,'" + userName + "','" + password +"')";

//        PreparedStatement pstmt = con.prepareStatement(sql);
//        pstmt.setString(1,userName);
//        pstmt.setString(2,password);
        Statement stmt = con.createStatement();
        stmt.executeUpdate(sql);
        int count = 0;
        count = stmt.executeUpdate(sql);
        if(count > 0) {
            System.out.println("添加成功");
            return true;
        }
        else {
            System.out.println("添加失败");
            return false;
        }
    }
    public boolean deleteUser(Connection con,String userName,String password) throws SQLException {
        User resultUser = new User();
        String sql = "SELECT * FROM USER WHERE userName = '" + userName +"' and password = '" + password +"'";
        String DELETEsql = "DELETE FROM USER  WHERE userName = '" + userName +"' and password = '" + password +"'";

//        PreparedStatement pstmt = con.prepareStatement(sql);
//        pstmt.setString(1,userName);
//        pstmt.setString(2,password);
        Statement stmt = con.createStatement();
        ResultSet resultSet = stmt.executeQuery(sql);
        if(resultSet == null) {
            System.out.println("未查找到此用户");
            return false;
        }
        else {
            stmt.executeUpdate(DELETEsql);
            System.out.println("用户已删除");
            return true;
        }
    }
}
